package com.thinvent.nj.ais.controller.sys;

import com.thinvent.nj.common.page.Page;
import com.thinvent.nj.common.rest.ResponseEntity;
import com.thinvent.nj.common.util.MapperUtil;
import com.thinvent.nj.ais.entity.Server;
import com.thinvent.nj.ais.service.ServerService;
import com.thinvent.nj.web.controller.BaseViewController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
*
* @author liupj
* @date 2018-4-24
*/
@Controller
@RequestMapping(path = "/sys/servers")
public class ServerController extends BaseViewController {

    @Autowired
    private ServerService serverService;

    @RequiresPermissions("server:view")
    @RequestMapping(method = RequestMethod.GET)
    public String toHtml() {
        return "sys/serverList";
    }


    @RequestMapping(path = "/search/page", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity findTableData(@RequestBody Map<String, Object> params) {
        Page<Server> target = serverService.findPage(getQueryParameter(params), (Map<String, Object>)params.get("queryMsg"));
        return ResponseEntity.ok(target);
    }

    @RequestMapping(path = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity get(@PathVariable("id") String id) {
        return ResponseEntity.ok(serverService.get(id));
    }

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity insert(@RequestBody Map<String, Object> params) {
        Server target = new Server();
        MapperUtil.copy(params, target);

        serverService.insert(target);

        return ResponseEntity.ok();
    }


    @RequestMapping(path = "/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseEntity update(@PathVariable("id") String id, @RequestBody Map<String, Object> params) {
        Server target = serverService.get(id);
        MapperUtil.copy(params, target);

        serverService.update(target);

        return ResponseEntity.ok();
    }

    @RequestMapping(path = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ResponseEntity del(@PathVariable("id") String id) {
        serverService.delete(id);
        return ResponseEntity.ok();
    }



}
